/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.gbvbahia.vendas.modelos;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author guilherme
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Cliente.buscarClientesPorTelefone",
            query = "Select distinct c From Cliente c left join c.telefones t"
            + " Where (:telefone2 = 'todos' OR t.telefone like :telefone) "
            + " AND (:nome2 = 'todos' OR c.nome like :nome)"
            + " Order by c.id "),
    @NamedQuery(name = "Cliente.contarClientesPorTelefone",
            query = "Select count(distinct c) From Cliente c left join c.telefones t"
            + " Where (:telefone2 = 'todos' OR t.telefone like :telefone) "
            + " AND (:nome2 = 'todos' OR c.nome like :nome)")
})
@Table(name = "cliente")
public class Cliente implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    /*
     * 
     */
    @NotNull(message = "Informe o nome do cliente")
    @Size(max = 100, message = "O nome limitado a 100 letras")
    @Column(name = "nome", nullable = false, length = 100)
    private String nome;
    /*
     * 
     */
    @Column(name = "data_cadastro", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dataCadastro = new Date();
    /*
     * 
     */
    @OneToMany(targetEntity = Telefone.class, cascade = CascadeType.ALL)
    @JoinTable(name = "cliente_telefone", joinColumns =
            @JoinColumn(name = "id_cliente"), inverseJoinColumns =
            @JoinColumn(name = "id_telefone"))
    @NotNull(message = "Pelo menos um telefone deve ser informado")
    @Size(min = 1, message = "Pelo menos um telefone deve ser informado")
    private List<Telefone> telefones;
    /**
     *
     */
    @OneToMany(targetEntity = Endereco.class, cascade = CascadeType.ALL)
    @JoinTable(name = "cliente_endereco", joinColumns =
            @JoinColumn(name = "id_cliente"), inverseJoinColumns =
            @JoinColumn(name = "id_endereco"))
    @NotNull(message = "Pelo menos um endereco deve ser informado")
    @Size(min = 1, message = "Pelo menos um endereco deve ser informado")
    private List<Endereco> endereco;
    /*
     * 
     */
    @Version
    @Column(name = "versao")
    private Long versao;
    /*
     * 
     */
    @Transient
    private boolean marcado;
    /*
     * 
     */

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Cliente)) {
            return false;
        }
        Cliente other = (Cliente) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String tels = "";
        for (Telefone tel : getTelefones()) {
            tels += "(" + tel.getDdd() + ")" + tel.getTelefone() + " ";
        }
        return this.nome + " " + tels;
    }
    
    public String getLabel(){
        return toString();
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Date getDataCadastro() {
        return dataCadastro;
    }

    public void setDataCadastro(Date dataCadastro) {
        this.dataCadastro = dataCadastro;
    }

    public List<Telefone> getTelefones() {
        if (telefones == null) {
            telefones = new ArrayList<Telefone>();
        }
        return telefones;
    }

    public void setTelefones(List<Telefone> telefones) {
        this.telefones = telefones;
    }

    public List<Endereco> getEndereco() {
        if (endereco == null) {
            endereco = new ArrayList<Endereco>();
        }
        return endereco;
    }

    public void setEndereco(List<Endereco> endereco) {
        this.endereco = endereco;
    }

    public boolean isMarcado() {
        return marcado;
    }

    public void setMarcado(boolean marcado) {
        this.marcado = marcado;
    }
}
